/*
 * 
 * v1.2 by tonydon
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txcms.domain.Tagslib;
import cn.txdnet.txcms.query.TagslibQuery;
import cn.txdnet.txcms.service.TagslibService;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.WebUtil;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author txdnet
 */
public class TagsAction {

    /*
     * 建立新标签
     */
    public ModelView make(HttpServletRequest req) throws Exception {
        Tagslib tag = BeanUtil.parseRequestToBean(Tagslib.class, req);
        if (!tag.validate()) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        if (TagslibService.make(tag)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /*
     * 删除标签
     */
    public ModelView delete(HttpServletRequest req) throws Exception {
        int tagid = WebUtil.getInt(req, "id", -1);
        if (tagid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        if (TagslibService.delete(tagid)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新类别
     *
     * @param req
     * @return
     */
    public ModelView update_channel_catelog(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        int cid = WebUtil.getInt(req, "cid", -1);
        String ctp = WebUtil.getNoNull(req, "ctp", null);
        byte channeltype = WebUtil.getByte(req, "channeltype", -1);
        if (ids == null || cid < 0 || ctp == null || channeltype < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = TagslibService.updateChannelAndCatalogBatch(ids, channeltype, cid, ctp);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新发布状态
     *
     * @param req
     * @return
     */
    public ModelView update_pubstate(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte pubstate = WebUtil.getByte(req, "pubstate", -1);
        if (ids == null || pubstate < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = TagslibService.updatePubstateBatch(ids, pubstate);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新 tag name
     * @param req
     * @return 
     */
    public ModelView update_tagname(HttpServletRequest req) {
        int tagid = WebUtil.getInt(req, "tagid", -1);
        String tagname = WebUtil.getNoNull(req, "tagname", null);
        if (tagid < 0 || tagname == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        if (TagslibService.updateTagname(tagid, tagname)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }
    /*
     * 查询标签
     */

    public ModelView list_factor(HttpServletRequest req) throws Exception {
        TagslibQuery query = BeanUtil.parseRequestToBean(TagslibQuery.class, req);
        return ModelUtil.getJsonModel(TagslibService.queryByFactor(query));
    }

    /**
     * 查询类目tag用于info绑定
     *
     * @param req
     * @return
     * @throws Exception
     */
    public ModelView query_list(HttpServletRequest req) throws Exception {
        int cid = WebUtil.getInt(req, "cid", -1);
        if (cid < 0) {
            return ModelUtil.getJsonModel(Collections.emptyList());
        }
        return ModelUtil.getJsonModel(TagslibService.queryList(cid));
    }
}
